// // Solution using chars to store each entry // unsigned int Ids[10000001]; unsigned char Entries[10000001][6]; int CompareEntries( unsigned char *p, unsigned char *q) { int Count = 0; if ((p[0] == q[0]) || (p[0] == q[1]) || (p[0] == q[2]) || (p[0] == q[3]) || (p[0] == q[4]) || (p[0] == q[5])) { Count += 1; } if ((p[1] == q[0]) || (p[1] == q[1]) || (p[1] == q[2]) || (p[1] == q[3]) || (p[1] == q[4]) || (p[1] == q[5])) { Count += 1; } if ((p[2] == q[0]) || (p[2] == q[1]) || (p[2] == q[2]) || (p[2] == q[3]) || (p[2] == q[4]) || (p[2] == q[5])) { Count += 1; } if ((p[3] == q[0]) || (p[3] == q[1]) || (p[3] == q[2]) || (p[3] == q[3]) || (p[3] == q[4]) || (p[3] == q[5])) { Count += 1; } if ((p[4] == q[0]) || (p[4] == q[1]) || (p[4] == q[2]) || (p[4] == q[3]) || (p[4] == q[4]) || (p[4] == q[5])) { Count += 1; } if ((p[5] == q[0]) || (p[5] == q[1]) || (p[5] == q[2]) || (p[5] == q[3]) || (p[5] == q[4]) || (p[5] == q[5])) { Count += 1; } return Count; } // // Solution using a bitvector to store each entry // unsigned int Ids[10000001]; unsigned char Entries[10000001][7]; // // Bit macros and routines // #define SetBit(X,B) { *((X)+((B)-1)/8) |= 1 << ((B)-1)%8; } // // An array that indicates how many bits are set in a given byte // unsigned char Bitset[] = { /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */ /* 0 */ 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, /* 1 */ 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, /* 2 */ 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, /* 3 */ 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, /* 4 */ 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, /* 5 */ 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, /* 6 */ 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, /* 7 */ 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, /* 8 */ 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, /* 9 */ 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, /* a */ 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, /* b */ 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, /* c */ 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, /* d */ 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, /* e */ 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, /* f */ 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8 }; // // A routine to compare two entries and returns how bits // match // int CompareEntries(unsigned char *p, unsigned char *q) { int c; c = Bitset[p[0] & q[0]]; c += Bitset[p[1] & q[1]]; c += Bitset[p[2] & q[2]]; c += Bitset[p[3] & q[3]]; c += Bitset[p[4] & q[4]]; c += Bitset[p[5] & q[5]]; c += Bitset[p[6] & q[6]]; return c; }